<!-- This comment will put IE 6, 7 and 8 in quirks mode -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>examples/Unsupervised/PCA.cpp Source File</title>
<script type="text/javaScript" src="search/search.js"></script>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3.0.1/es5/tex-mml-chtml.js"></script>
<script src="../../mlstyle.js"></script>
<link href="../css/besser.css" rel="stylesheet" type="text/css"/>
</head>
<!-- pretty cool: each body gets an id tag which is the basename of the web page  -->
<!--              and allows for page-specific CSS. this is client-side scripted, -->
<!--              so the id will not yet show up in the served source code -->
<script type="text/javascript">
    jQuery(document).ready(function () {
        var url = jQuery(location).attr('href');
        var pname = url.substr(url.lastIndexOf("/")+1, url.lastIndexOf(".")-url.lastIndexOf("/")-1);
        jQuery('#this_url').html('<strong>' + pname + '</strong>');
        jQuery('body').attr('id', pname);
    });
</script>
<body>
    <div id="shark_old">
        <div id="wrap">
            <div id="header">
                <div id="site-name"><a href="../../sphinx_pages/build/html/index.html">Shark machine learning library</a></div>
                <ul id="nav">
                    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/installation.html">Installation</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/tutorials/tutorials.html">Tutorials</a>
                    </li>
		    <li >
                        <a href="../../sphinx_pages/build/html/rest_sources/benchmark.html">Benchmarks</a>
                    </li>
                    <li class="active">
                        <a href="classes.html">Documentation</a>
                        <ul>
                            <li class="first"></li>
                            <li><a href="../../sphinx_pages/build/html/rest_sources/quickref/quickref.html">Quick references</a></li>
                            <li><a href="classes.html">Class list</a></li>
                            <li class="last"><a href="group__shark__globals.html">Global functions</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </div>
<div id="doxywrapper">
<!--
    <div id="global_doxytitle">Doxygen<br>Documentation:</div>
-->
    <div id="navrow_wrapper">
<!-- Generated by Doxygen 1.9.8 -->
<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="dir_d28a4824dc47e487b107a5db32ef43c4.html">examples</a></li><li class="navelem"><a class="el" href="dir_f3b52807f92d96d3463bd1ae3c5a9f93.html">Unsupervised</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle"><div class="title">PCA.cpp</div></div>
</div><!--header-->
<div class="contents">
<a href="_p_c_a_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno">    1</span><span class="preprocessor">#include &lt;<a class="code" href="_p_c_a_8h.html">shark/Algorithms/Trainers/PCA.h</a>&gt;</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno">    2</span> </div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno">    3</span><span class="comment">//header needed for data generation</span></div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno">    4</span><span class="preprocessor">#include &lt;<a class="code" href="_multi_variate_normal_distribution_8h.html">shark/Statistics/Distributions/MultiVariateNormalDistribution.h</a>&gt;</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno">    5</span> </div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno">    6</span><span class="keyword">using namespace </span><a class="code hl_namespace" href="namespaceshark.html" title="AbstractMultiObjectiveOptimizer.">shark</a>;</div>
<div class="line"><a id="l00007" name="l00007"></a><span class="lineno">    7</span><span class="keyword">using namespace </span>std;</div>
<div class="line"><a id="l00008" name="l00008"></a><span class="lineno">    8</span><span class="comment"></span> </div>
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno">    9</span><span class="comment">///In this test, we will use PCA to calculate the</span></div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno">   10</span><span class="comment">///eigenvectors of a scatter matrix and do a</span></div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno">   11</span><span class="comment">///reduction of the subspace to the space</span></div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno">   12</span><span class="comment">///spanned by the two eigenvectors with the biggest</span></div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno">   13</span><span class="comment">///eigenvalues.</span></div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno">   14</span><span class="comment"></span><span class="comment"></span> </div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno">   15</span><span class="comment">///the principal components of our multivariate data distribution</span></div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno">   16</span><span class="comment">///we will use them later for checking</span></div>
<div class="foldopen" id="foldopen00017" data-start="{" data-end="};">
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno"><a class="line" href="_p_c_a_8cpp.html#a7883ae9c3cb096c720995c7c622d392b">   17</a></span><span class="comment"></span><span class="keywordtype">double</span> <a class="code hl_variable" href="_p_c_a_8cpp.html#a7883ae9c3cb096c720995c7c622d392b">principalComponents</a>[3][3] =</div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno">   18</span>{</div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno">   19</span>    { 5, 0, 0},</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno">   20</span>    { 0, 2, 2},</div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno">   21</span>    { 0,-1, 1}</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno">   22</span>};</div>
</div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno">   23</span> </div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno"><a class="line" href="_p_c_a_8cpp.html#aeaeb0a774c6611bbc82b86d4aa9ece28">   24</a></span>std::size_t <a class="code hl_variable" href="_p_c_a_8cpp.html#aeaeb0a774c6611bbc82b86d4aa9ece28">numberOfExamples</a> = 30000;</div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno">   25</span><span class="comment"></span> </div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno">   26</span><span class="comment">///The test distribution is just a multivariate Gaussian.</span></div>
<div class="foldopen" id="foldopen00027" data-start="{" data-end="}">
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno"><a class="line" href="_p_c_a_8cpp.html#a642f675b882ffab29fa85acf04830e30">   27</a></span><span class="comment"></span><a class="code hl_class" href="classshark_1_1_unlabeled_data.html" title="Data set for unsupervised learning.">UnlabeledData&lt;RealVector&gt;</a> <a class="code hl_function" href="_p_c_a_8cpp.html#a642f675b882ffab29fa85acf04830e30" title="The test distribution is just a multivariate Gaussian.">createData</a>()</div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno">   28</span>{</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno">   29</span>    RealVector <a class="code hl_function" href="namespaceshark.html#a6ae694efca57e84792fcff090223437e" title="Calculates the mean vector of the input vectors.">mean</a>(3);</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno">   30</span>    <a class="code hl_function" href="namespaceshark.html#a6ae694efca57e84792fcff090223437e" title="Calculates the mean vector of the input vectors.">mean</a>(0) = 1;</div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno">   31</span>    <a class="code hl_function" href="namespaceshark.html#a6ae694efca57e84792fcff090223437e" title="Calculates the mean vector of the input vectors.">mean</a>(1) = -1;</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno">   32</span>    <a class="code hl_function" href="namespaceshark.html#a6ae694efca57e84792fcff090223437e" title="Calculates the mean vector of the input vectors.">mean</a>(2) = 3;</div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno">   33</span> </div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno">   34</span>    <span class="comment">// to create the covariance matrix we first put the</span></div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno">   35</span>    <span class="comment">// copy the principal components  in the matrix</span></div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno">   36</span>    <span class="comment">// and than use an outer product</span></div>
<div class="line"><a id="l00037" name="l00037"></a><span class="lineno">   37</span>    RealMatrix <a class="code hl_function" href="namespaceshark.html#a0596df3c2544cbca51ec485254c27448" title="Calculates the covariance matrix of the data vectors.">covariance</a>(3,3);</div>
<div class="line"><a id="l00038" name="l00038"></a><span class="lineno">   38</span>    <span class="keywordflow">for</span>(<span class="keywordtype">int</span> i = 0; i != 3; ++i)</div>
<div class="line"><a id="l00039" name="l00039"></a><span class="lineno">   39</span>    {</div>
<div class="line"><a id="l00040" name="l00040"></a><span class="lineno">   40</span>        <span class="keywordflow">for</span>(<span class="keywordtype">int</span> j = 0; j != 3; ++j)</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno">   41</span>        {</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno">   42</span>            <a class="code hl_function" href="namespaceshark.html#a0596df3c2544cbca51ec485254c27448" title="Calculates the covariance matrix of the data vectors.">covariance</a>(i,j) = <a class="code hl_variable" href="_p_c_a_8cpp.html#a7883ae9c3cb096c720995c7c622d392b">principalComponents</a>[i][j];</div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno">   43</span>        }</div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno">   44</span>    }</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno">   45</span>    <a class="code hl_function" href="namespaceshark.html#a0596df3c2544cbca51ec485254c27448" title="Calculates the covariance matrix of the data vectors.">covariance</a> = prod(trans(<a class="code hl_function" href="namespaceshark.html#a0596df3c2544cbca51ec485254c27448" title="Calculates the covariance matrix of the data vectors.">covariance</a>),<a class="code hl_function" href="namespaceshark.html#a0596df3c2544cbca51ec485254c27448" title="Calculates the covariance matrix of the data vectors.">covariance</a>);</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno">   46</span> </div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno">   47</span>    <span class="comment">//now we can create the distribution</span></div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno">   48</span>    <a class="code hl_class" href="classshark_1_1_multi_variate_normal_distribution.html" title="Implements a multi-variate normal distribution with zero mean.">MultiVariateNormalDistribution</a> distribution(<a class="code hl_function" href="namespaceshark.html#a0596df3c2544cbca51ec485254c27448" title="Calculates the covariance matrix of the data vectors.">covariance</a>);</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno">   49</span> </div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno">   50</span>    <span class="comment">//and we sample from it</span></div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno">   51</span>    std::vector&lt;RealVector&gt; data(<a class="code hl_variable" href="_p_c_a_8cpp.html#aeaeb0a774c6611bbc82b86d4aa9ece28">numberOfExamples</a>);</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno">   52</span>    <span class="keywordflow">for</span> (<span class="keyword">auto</span>&amp; sample: data)</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno">   53</span>    {</div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno">   54</span>        <span class="comment">//first element is the sample, second is the underlying uniform gaussian</span></div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno">   55</span>        sample = <a class="code hl_function" href="namespaceshark.html#a6ae694efca57e84792fcff090223437e" title="Calculates the mean vector of the input vectors.">mean</a> + distribution(<a class="code hl_variable" href="namespaceshark_1_1random.html#ab5c1547eee483974d008d43f621a2234">random::globalRng</a>).first;</div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno">   56</span>    }</div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno">   57</span>    <span class="keywordflow">return</span> <a class="code hl_function" href="group__shark__globals.html#ga1a1a4f4249f709e6169a601a9a857fa8" title="creates a data object from a range of elements">createDataFromRange</a>(data);</div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno">   58</span>}</div>
</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno">   59</span> </div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno">   60</span> </div>
<div class="foldopen" id="foldopen00061" data-start="{" data-end="}">
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"><a class="line" href="_p_c_a_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">   61</a></span><span class="keywordtype">int</span> <a class="code hl_function" href="_p_c_a_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a>(){</div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno">   62</span> </div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno">   63</span>    <span class="comment">// We first create our problem. Since the PCA is a unsupervised Method,</span></div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno">   64</span>    <span class="comment">// We use UnlabeledData instead of Datas. </span></div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno">   65</span>    <a class="code hl_class" href="classshark_1_1_unlabeled_data.html" title="Data set for unsupervised learning.">UnlabeledData&lt;RealVector&gt;</a> data = <a class="code hl_function" href="_p_c_a_8cpp.html#a642f675b882ffab29fa85acf04830e30" title="The test distribution is just a multivariate Gaussian.">createData</a>();</div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno">   66</span> </div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno">   67</span>    <span class="comment">// With the definition of the model, we declare, how many</span></div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno">   68</span>    <span class="comment">// principal components we want.  If we want all, we set</span></div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno">   69</span>    <span class="comment">// inputs=outputs = 3, but since want to do a reduction, we</span></div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno">   70</span>    <span class="comment">// use only 2 in the second argument.  The third argument is</span></div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno">   71</span>    <span class="comment">// the bias. pca needs a bias to work.</span></div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno">   72</span>    <a class="code hl_class" href="classshark_1_1_linear_model.html" title="Linear Prediction with optional activation function.">LinearModel&lt;&gt;</a> pcaModel(3,2,<span class="keyword">true</span>);</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno">   73</span> </div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno">   74</span>    <span class="comment">// Now we can construct the PCA.</span></div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno">   75</span>    <span class="comment">// We can decide whether we want a whitened output or not.</span></div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno">   76</span>    <span class="comment">// For testing purposes, we don&#39;t want whitening in this</span></div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno">   77</span>    <span class="comment">// example.</span></div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno">   78</span>    <a class="code hl_class" href="classshark_1_1_p_c_a.html" title="Principal Component Analysis.">PCA</a> pca;</div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno">   79</span>    pca.<a class="code hl_function" href="classshark_1_1_p_c_a.html#a13027f51bff74d5b7a39d4040a9aa403">setWhitening</a>(<span class="keyword">false</span>);</div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno">   80</span>    pca.<a class="code hl_function" href="classshark_1_1_p_c_a.html#aeae45267c5bc7a3cda484b203a1f15be">train</a>(pcaModel,data);</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno">   81</span> </div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno">   82</span> </div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno">   83</span> </div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno">   84</span>    <span class="comment">//Print the rescaled results.</span></div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno">   85</span>    <span class="comment">//Should be the same as principalComponents, except for sign change</span></div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno">   86</span>    <span class="comment">//and numerical errors.</span></div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno">   87</span>    cout &lt;&lt; <span class="stringliteral">&quot;RESULTS: &quot;</span> &lt;&lt; std::endl;</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno">   88</span>    cout &lt;&lt; <span class="stringliteral">&quot;======== &quot;</span> &lt;&lt; std::endl &lt;&lt; std::endl;</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno">   89</span>    cout &lt;&lt; <span class="stringliteral">&quot;principal component 1: &quot;</span> &lt;&lt; row(pcaModel.<a class="code hl_function" href="classshark_1_1_linear_model.html#ad16f3372ed0f7b3aa3c13c5519c7f6a4" title="return a copy of the matrix in dense format">matrix</a>(),0) * sqrt(pca.<a class="code hl_function" href="classshark_1_1_p_c_a.html#a8a377bad66488acb59ab44a3c7ee21ea">eigenvalues</a>()(0)) &lt;&lt; std::endl;</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno">   90</span>    cout &lt;&lt; <span class="stringliteral">&quot;principal component 2: &quot;</span> &lt;&lt; row(pcaModel.<a class="code hl_function" href="classshark_1_1_linear_model.html#ad16f3372ed0f7b3aa3c13c5519c7f6a4" title="return a copy of the matrix in dense format">matrix</a>(),1) * sqrt( pca.<a class="code hl_function" href="classshark_1_1_p_c_a.html#a8a377bad66488acb59ab44a3c7ee21ea">eigenvalues</a>()(1) ) &lt;&lt; std::endl;</div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno">   91</span> </div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno">   92</span>}</div>
</div>
</div><!-- fragment --></div><!-- contents -->
</div>
</body>
</html>
